package com.github.linkoog.model_designer.ide;

import com.intellij.openapi.ui.DialogWrapper;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;

public class MessageDialog extends DialogWrapper {

    private Component parent;
    private JPanel myMainPanel;
    private JLabel messageLable;
    private JPanel contextPanel;
    private Callback callback;

    protected MessageDialog(Component parent, String title, String message, Callback callback) {
        super( null,  parent,false,  IdeModalityType.IDE);
        this.parent = parent;
        //this.setResizable(false);
        this.callback = callback;
        this.myMainPanel.setPreferredSize(new Dimension(230, 80));

        if (StringUtils.isNotBlank(title)){
            this.setTitle(title);
        }

        if (StringUtils.isNotBlank(message)){
            messageLable.setText(message);
        }
        init();
    }


    @Override
    public void doCancelAction() {
        super.doCancelAction();
    }

    @Override
    public void dispose() {
        super.dispose();
    }

    @Override
    public void doOKAction() {
        super.doOKAction();
        if (callback != null){
            callback.apply();
        }
    }

    public static Builder builder() {
        return new Builder();
    }

    @Override
    protected JPanel createCenterPanel() {
        return myMainPanel;
    }

    public JPanel getMyMainPanel() {
        return myMainPanel;
    }


    public static class Builder {
        private Component parent;
        private String title;
        private String message;
        private Callback callback;

        public Builder parent(Component parent) {
            this.parent = parent;
            return this;
        }

        public Builder title(String title) {
            this.title = title;
            return this;
        }

        public Builder message(String message) {
            this.message = message;
            return this;
        }

        public Builder callback(Callback callback) {
            this.callback = callback;
            return this;
        }

        public MessageDialog build() {
            MessageDialog messageDialog = new MessageDialog(parent, title, message, callback);
            return messageDialog;
        }
    }

    @FunctionalInterface
    public static interface Callback{
        void apply();
    }
}
